//
//  NewFeatureViewController.swift
//  Weibo
//
//  Created by 李振兴 on 15/10/19.
//  Copyright © 2015年 李振兴. All rights reserved.
//

import UIKit
import SnapKit
/// 新特性图像的数量
private let WBNewFeatureImageCount = 4

private let reuseIdentifier = "Cell"

class NewFeatureViewController: UICollectionViewController {
    init(){
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.scrollDirection = .Horizontal
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        // 构造函数，完成之后内部属性才会被创建
        super.init(collectionViewLayout: layout)
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
                // Register cell classes
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return 4
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        cell.imageIndex = indexPath.row
        
        return cell
    }
    ///滑动结束
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        if page != WBNewFeatureImageCount-1 {
            return
        }
       let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forRow: page, inSection: 0)) as? NewFeatureCell
        cell?.showStartAni()
    }
}

private class NewFeatureCell:UICollectionViewCell{
    /// 点击开始体验按钮
    @objc private func clickStartButton() {
        print("开始体验")
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification, object: nil)

    }
    
    var imageIndex:Int = 0 {
        didSet{
            startButton.hidden = true
            imageView.image = UIImage(named: "new_feature_\(imageIndex+1)")
        }
    }
    ///开始动画
    private func showStartAni(){
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        startButton.userInteractionEnabled = false
        UIView.animateWithDuration(1.6,
            delay: 0,
            usingSpringWithDamping: 0.6,
            initialSpringVelocity: 10,
            options: [],
            animations: { () -> Void in
                self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                self.startButton.userInteractionEnabled = true
                print("ok")
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI(){
        self.contentView.addSubview(imageView)
        self.contentView.addSubview(startButton)
        
        imageView.frame = bounds
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.bottom.equalTo(self.snp_bottom).multipliedBy(0.7)
        }
        // 3. 监听方法
        startButton.addTarget(self, action: "clickStartButton", forControlEvents: .TouchUpInside)
        
    }
    
    private lazy var imageView:UIImageView = UIImageView()
    /// 开始体验按钮
    private lazy var startButton: UIButton = UIButton(title: "开始体验", color: UIColor.whiteColor(), backImageName: "new_feature_finish_button")
}